﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;
using System.Collections;

namespace CMD.BAL.AUTH
{
    [DataContractAttribute]
    public class Rol
    {
        [DataMemberAttribute]
        public Group group;
        [DataMemberAttribute]
        public string idRol;
        [DataMemberAttribute]
        public int prio;
        [DataMemberAttribute]
        public string rol;

        //public static CMD.BAL.MISC.UpsertStateMsg AddGroup(string IdRol, Group group)
        //{
        //    CMD.BAL.MISC.UpsertStateMsg add;
        //    return add;
        //}
        public static CMD.BAL.MISC.DeleteStateMsg DeleteRol(string[] aIdRol)
        {
            CMD.BAL.MISC.DeleteStateMsg delete;
            //convirto
            string join = String.Join(", ", aIdRol);
            //ejecuto el store
            try 
        	{
                int dt = CMD_WEB.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_DELETEROL'" + join + "'");
                delete = new MISC.DeleteStateMsg("registros eliminados " + dt.ToString(),true);
                return delete;
	        }
	        catch (Exception ex)
	        {
               delete = new MISC.DeleteStateMsg("error al eliminar" + ex, false);
		    return delete;
	        }
        }
        public static List<Rol> GetDataRol(string[] aIdRol) 
        {
            string join = String.Join(", ", aIdRol);
            DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GETROL'" + join + "'");
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                List<Rol> list = dt.AsEnumerable().Select(
                    row => new Rol
                    {
                        idRol = String.IsNullOrEmpty(row.Field<string>(0))
                        ? ""
                        : row.Field<string>(0),
                        prio = row.Field<int?>(2).GetValueOrDefault(),
                        rol = String.IsNullOrEmpty(row.Field<string>(1))
                        ? ""
                        : row.Field<string>(1),
                    }
                    ).ToList();
                return list;
            }
        }
        public static CMD.BAL.MISC.UpsertStateMsg UpsertRol(Rol data)   
        {
            Rol inser;
            CMD.BAL.MISC.UpsertStateMsg insertRol;
            try
            {
                int dt = CMD_WEB.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_UPSERTROL'" + data.idRol + "','" + data.prio.ToString() + "','" + data.rol + "'");
                inser = new Rol(data.idRol,data.prio,data.rol);
                insertRol = new MISC.UpsertStateMsg("registros guardados " + dt.ToString(), true);
                return insertRol;
            }
            catch (Exception ex)    
            {
                insertRol = new MISC.UpsertStateMsg("error al guardar " + ex, false);
                return insertRol;
            }
        }
        public Rol()
        {
            idRol = "";
            prio = 0;
            rol = "";
        }
        public Rol(string idRol, int prio, string rol) 
        {
            this.idRol =idRol;
            this.prio = prio;
            this.rol = rol;
        }
    }
}
